
#ifndef LOD_MAIN_DISP_H
#define LOD_MAIN_DISP_H

#include <stdlib.h>
#include "disp.h"
#include "../light/lighting.h"

#include <GL/glew.h>
#include <GL/glut.h>
#include <vector>
#include <string>

// todo
#include <iostream>
using std::cout;
using std::flush;


class main_disp : public disp
{
    public:
    main_disp(void* font, const GLfloat* menu_color4, const GLfloat* act_menu_color4, 
              int Width, int Height, double Fovy)
     : disp(font, menu_color4, act_menu_color4, main_disp::text, main_disp::yinc_text, 
            main_disp::xoffset_text, main_disp::show_text, main_disp::menu_pos,
            main_disp::text_count),
       li(0), // no lights, but a lighting control 
       width(Width),
       height(Height),
       fovy(Fovy) // unused at the moment
    {
        //reshape(width, height);
        set_act_menu_pos(START_ACT_MENU_POS);
        //set_show_help(HELP_SHOWED_ON_START);
    }


    virtual void display(int width, int height)
    {
        li.disable();
        glColor3f(1,1,1);
        quick_draw_string(width,height,"Pressing h at each screen gives a list of key shortcuts",20,20);
        disp_text(width, height);
    }



    virtual disp* keyboard(unsigned char key, int x, int y)
    {
        disp* menu = this;
        switch(key)
        {
            case 'q':
            case 'Q':
                this->quit();
                break;
            case 'w':
                this->act_menu_pos_up();
                break;
            case 's':
                this->act_menu_pos_down();
                break;
            //case 'h':
            //    this->swap_help_display();
            //    break;

            //case '\n':
            //    // in case both '\n' and '\r' are generated for enter key
            //    break;
            // strange observation: glut gives just '\r'
            // as a newline character under Linux
            case '\r':
                menu = this->enter_key();
                break;
        }
        return menu;
    }

    virtual void keyboard_special(int key, int x, int y)
    {
        switch(key) {
            case GLUT_KEY_UP:
                this->act_menu_pos_up();
                break;
            case GLUT_KEY_DOWN:
                this->act_menu_pos_down();
                break;
        }
    }

    //! When window size changes, we have to adjust its displays' view
    virtual void reshape(int width, int height)
    {
        set_xstart_text(width/3);
        set_ystart_text(int(height * 2/3.0));

        if(this->width!=width || this->height!=height) {
            this->width = width;
            this->height = height;
        }
    }

    virtual void init()
    {
        glViewport(0, 0, (GLsizei) width, (GLsizei) height);
        // set projection matrix, if necessary
    }


    private:
    void quit()
    {
        exit(0);
    }

    disp* enter_key()
    {
        if(get_act_menu_pos() == text_count-1) { // the very bottom position
            quit();
            return this; // only to fake the compiler
        }
        else {
            return change_menu(true); // go to the chosen menu position
            //return this;
        }
    }

    lighting li;
    int width;
    int height;
    double fovy;
    
    static const char* text[];
    static const int yinc_text[];
    static const int xoffset_text[];
    static bool show_text[];
    static bool menu_pos[];
    static const int text_count;

    static const short START_ACT_MENU_POS;// = 0;
    //static const bool HELP_SHOWED_ON_START;// = false;
};

#endif // LOD_MAIN_DISP_H
